package com.processmining.support.analyseSupport.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.ProcessMining.mining.heuristics.HeuristicsMetrics;
import net.ProcessMining.models.heuristics.SimpleHeuristicsNet;


/**
 * 桑基图表示事件间的频次，持续时间
 * 
 * @author admin
 * 
 */
public class SankeyDiagram {
	private List<Integer> startIndexs;
	private List<SankeyActivity> sankeyActivities;
	private SimpleHeuristicsNet net;

	public SankeyDiagram(SimpleHeuristicsNet net) {
		super();
		this.net = net;
		startIndexs=new ArrayList<Integer>();
		sankeyActivities=new ArrayList<SankeyActivity>();
	}

	public void createSankeyDiagram() {
		HeuristicsMetrics metrics = this.net.getMetrics();
		int size = net.size();System.out.println("EventClasses "+size);
		Map<Integer, String> names = new HashMap<Integer, String>();
		for (int i = 0; i < size; i++) {
			names.put(i, net.getActivitiesMappingStructures()
					.getXEventClasses().getByIndex(i).toString());
		}
		for (int i = 0; i < size; i++) {
			String name = names.get(i);
			SankeyActivity activity = new SankeyActivity(i, name);
			// 判断是否为开始事件
			int inputSetSize = net.getInputSet(i).size();
			if (inputSetSize == 0) {
				startIndexs.add(i);
			}
			// 组织活动的output
			int outputSetSize = net.getOutputSet(i).size();
			for (int j = 0; j < outputSetSize; j++) {
				int subSetSize = net.getOutputSet(i).get(j).size();
				List<SankeyItem> outputSubList = new ArrayList<SankeyItem>();
				for (int k = 0; k < subSetSize; k++) {
					int element = net.getOutputSet(i).get(j).get(k);
					SankeyItem item = new SankeyItem(
							element,names.get(element),
							(long) metrics.getDirectSuccessionCount(i, element),
							(long) metrics.getAvgTimes(i, element),
							(long) metrics.getBestTimes(i, element),
							(long) metrics.getWorstTimes(i, element));
					outputSubList.add(item);
				}
				activity.getOutputs().put(j, outputSubList);
			}
			sankeyActivities.add(activity);
		}
	}

	@Override
	public String toString() {
		StringBuffer sb=new StringBuffer();
		sb.append("startIndexs:");
		for(int index:startIndexs){
			sb.append(index).append(";");
		}
		sb.append("\n");
		sb.append("sankeyActivities\n");
		for(SankeyActivity activity:sankeyActivities){
			sb.append(activity.getElement()).append(" ").append(activity.getName()).append("\n");
			Map<Integer, List<SankeyItem>> map=activity.getOutputs();
			if(map.size()>0){
				Set<Integer> keySet=activity.getOutputs().keySet();  
			       Iterator<Integer> iterator=keySet.iterator();  
			       while(iterator.hasNext()){  
			           int key=iterator.next();  
			           List<SankeyItem> val=map.get(key);  
			           sb.append("key-->"+key);
			           sb.append(" value-->\n");
			           for(SankeyItem item:val){
			        	   sb.append(item.toString());
			        	   sb.append("\n");
			           }
			             
			       }  
			}
		}
		return sb.toString();
	}

	public List<SankeyActivity> getSankeyActivities() {
		return sankeyActivities;
	}
	
}
